<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body>
<div id="pageloading" class="l-loading" style="display:block"></div>
<div class="layui-fluid">
    <!-- breadcrumb -->
    <div class="ad-breadcrumb">
        <div class="layui-breadcrumb" id="crumbs">
        </div>
        <div class="title">查询表格</div>
    </div>

    <!-- search -->
    <div class="ad-search">
        <div id="searchGroup">
        </div>
    </div>

    <!-- main -->
    <div class="layui-card ad-table-card" id="ad-table-card">
        <div class="layui-card-header">
            <div class="layui-table-title">
                <h2>表格查询</h2>
                <div class="layui-table-option layui-form demoTable">
                    <div id="tableDataBtn"></div>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div class="layui-col-md12">
                <table class="layui-hide" id="table1" lay-filter="tableFilter"></table>
                <script type="text/html" id="barTools">
                    <a class="ad-btn-link" lay-event="edit">编辑</a>
                    <a class="ad-btn-link" lay-event="roleMenu">菜单设置</a>
                    <a class="ad-btn-link" lay-event="del">删除</a>
                </script>
            </div>
        </div>

    </div>
</div>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var tableIns;

    ;!function(){
        var table = layui.table
            ,form = layui.form;

        tableIns = table.render({
            elem: '#table1'
            ,method: 'post'
            ,url: ctxPath + 'systemRole/list'
            , id: 'table1'
            , skin: 'line'
            , cellMinWidth: 100
            , limit: 20
            ,cols: [ [
                { type : 'checkbox' },
                { field:'name', title: '角色名', align : 'center'},
                { field:'note', title: '备注', align : 'center'},
                { field : '', title : '操作', width : 200, align : 'center', toolbar : '#barTools', fixed: 'right' }
            ] ]
            ,response: {
                statusName: 'searchCount'
                ,statusCode: true
                ,countName: 'total'
                ,dataName: 'records'
            }
            ,page: true
        });

        // 监听操作列
        table.on('tool(tableFilter)', function(obj) {
            if (obj.event == 'edit') {
                do_edit(obj);
            } else if (obj.event == "roleMenu") {
                do_menu(obj);
            } else if (obj.event == 'del') {
                do_delete(obj);
            }
        });

        //表格操作项按钮调用
        $("#tableDataBtn").tableDataBtn({
            data:[
                {id:'add', name:'新增', icon:'&#xe654;', dataType:'add'}
            ],
            batchData:[
                {id:'batch_del', name:'批量删除', icon:'&#xe640;', dataType:'batch_del'}
            ]
        })
        var active = {
            add: do_add
            ,batch_del: function(){
                var checkStatus = table.checkStatus('table1');
                do_batch_delete(checkStatus.data);
            }
        };
        $.actionFun(tableIns, active, form);

        //filter
        var filterJson = [
            {"text":"角色名称", "value": "name", "type":'input'}
        ]

        //查询
        $("#searchGroup").searchGroup({
            data: filterJson, //输入框的数据
            placeholder: true, //是否显示输入前提示文字
            //rows: 4 //每行显示个数
        });

        //查询表格
        form.on('submit(filterForm)', function(data) {
            var data = data.field;
            tableIns.reload({
                where : data
                ,page: {
                    curr: 1
                }
            });
            return false;
        });
    }();

    function do_add() {
        var url = ctxPath + "systemRole/add";
        var btns = [ '保存', '关闭' ];
        var btnFns = {
            yes : function(index, layero) {
                layero.find("iframe").contents().find("form [lay-submit]").click();
            }
        };
        openWin(850, 250, '角色信息-新增', url, btns, btnFns);
    }

    function do_edit(obj) {
        console.log(obj.data);
        var url = ctxPath + "systemRole/edit?roleId=" + obj.data.roleId;
        var btns = [ '保存', '关闭' ];
        var btnFns = {
            yes : function(index, layero) {
                layero.find("iframe").contents().find("form [lay-submit]").click();
            }
        };
        openWin(850, 250, '角色信息-修改', url, btns, btnFns);
    }

    function do_menu(obj) {
        var url = ctxPath + "systemRole/editMenu?roleId=" + obj.data.roleId;
        var btns = [ '保存', '关闭' ];
        var btnFns = {
            yes : function(index, layero) {
                layero.find("iframe").contents().find("form [lay-submit]").click();
            }
        };
        openWin(350, 500, '菜单设置', url, btns, btnFns);
    }

    function do_delete(obj) {
        layer.confirm('确定要删除"' + obj.data.name + '"角色吗？',
            {icon : 3}
            ,function(index) {
                layer.close(index);
                $.ajax({
                    type: 'post',
                    url : ctxPath + "systemRole/delete",
                    data : {
                        roleIds : obj.data.roleId
                    },
                    success: function(result) {
                        if (result) {
                            layer.msg("删除成功！", {
                                icon : 1
                                ,time : 1000
                            }, function() {
                                tableIns.reload();
                                $(".layui-table-alert").find("em").html(0);
                            });
                        }
                    }
                });
            }
        );
    }

    function do_batch_delete(obj) {
        if (obj.length == 0) {
            layer.msg('请选择至少一项');
            return false;
        }
        var pks = [];
        $.each(obj, function (index, data) {
            pks.push(data.roleId);
        });
        //后台删除数据
        layer.confirm("真的要删除选中的角色吗？", {
            icon : 3
        }, function(index) {
            layer.close(index);
            $.ajax({
                type: 'post',
                url : ctxPath + "systemRole/delete",
                data : {
                    roleIds : pks.join(",")
                },
                success : function(result) {
                    if (result) {
                        layer.msg("删除成功！", {
                            icon: 1
                            , time: 1000
                        }, function () {
                            tableIns.reload();
                            $(".layui-table-alert").find("em").html(0);
                        });
                    }
                }
            });
        });
    }

</script>